<?php

namespace Game\Controller;
use Common\Common\Library\Page;

class WxUserController extends CstBaseController
{
    const TABLE_NAME="WxUser";
    private $WxUserLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->WxUserLogic = A('Console/WxUser', 'Logic');
    }

    /**
     * 获取我发布的广告列表
     */
    public function doGetList()
    {
        $data = I('post.');
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 2;
        $phone=$data["phone"];
        unset($data['page']);
        unset($data['phone']);
        if(!empty($phone)){
            $data["phone"]=array('like',$phone+"%");
        }
        $listRes = $this->WxUserLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->WxUserLogic->getTypeDic();

        $this->assign("typeDic", $typeDic);
        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetWxUserList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }

    public function wxUserList()
    {
        $typeDic = $this->WxUserLogic->getTypeDic();
        $this->assign("typeDic", $typeDic);
        $this->display();
    }

     public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        vendor("PHPExcel.PHPExcel");
            
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

    /**
     *
     * 导出Excel
     */
    function expUser(){//导出Excel
        
        $xlsCell  = array(
        array('id','账号序列'),
        array('name','手机号'),
        array('name','微信号'),
        array('tname','次数')
        );
        $xlsModel = D(static::TABLE_NAME);
        $xlsData  = $xlsModel->Field('id,phone,WX_OPENID,times')->select();
        
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
            
    }
}